package com.ruyuan.process.engine.demo;

import com.ruyuan.process.engine.ProcessEngine;
import com.ruyuan.process.engine.config.ClassPathXmlProcessParser;
import com.ruyuan.process.engine.config.StringXmlProcessParser;
import com.ruyuan.process.engine.process.ProcessContext;

/**
 * @author zhonghuashishan
 * @version 1.0
 */
public class Application {
    public static void main(String[] args) throws Exception {
        usingClasspathXmlProcessParser();
        // usingStringXmlProcessParser();
    }

    private static void usingClasspathXmlProcessParser() throws Exception {
        ProcessEngine processEngine = new ProcessEngine(new ClassPathXmlProcessParser("process-demo.xml"));
        ProcessContext process1 = processEngine.getContext("process-async");
        process1.set("nextId", "node4");
        process1.start();
    }

    private static void usingStringXmlProcessParser() throws Exception {
        String xmlConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<process-context xmlns=\"http://www.w3school.com.cn\"\n" +
                "                 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "                 xsi:schemaLocation=\"http://www.w3school.com.cn process-engine.xsd\">\n" +
                "    <process name=\"process1\">\n" +
                "        <node name=\"node1\" class=\"com.ruyuan.process.engine.demo.StandardProcessorDemo\" next=\"node2\" begin=\"true\"/>\n" +
                "        <node name=\"node2\" class=\"com.ruyuan.process.engine.demo.RollBackProcessorDemo\" next=\"node3\"/>\n" +
                "        <node name=\"node3\" class=\"com.ruyuan.process.engine.demo.DynamicProcessorDemo\" next=\"node4,node5\"/>\n" +
                "        <node name=\"node4\" class=\"com.ruyuan.process.engine.demo.RollbackProcessorThrowExceptionDemo\"/>\n" +
                "        <node name=\"node5\" class=\"com.ruyuan.process.engine.demo.StandardProcessorDemo\"/>\n" +
                "    </process>\n" +
                "\n" +
                "    <process name=\"process-async\">\n" +
                "        <node name=\"node1\" class=\"com.ruyuan.process.engine.demo.StandardProcessorDemo\" next=\"node2\" begin=\"true\"/>\n" +
                "        <node name=\"node2\" class=\"com.ruyuan.process.engine.demo.RollBackProcessorDemo\" next=\"node3\"/>\n" +
                "        <node name=\"node3\" class=\"com.ruyuan.process.engine.demo.DynamicProcessorDemo\" next=\"node4,node5\" invoke-method=\"async\"/>\n" +
                "        <node name=\"node4\" class=\"com.ruyuan.process.engine.demo.RollbackProcessorThrowExceptionDemo\"/>\n" +
                "        <node name=\"node5\" class=\"com.ruyuan.process.engine.demo.StandardProcessorDemo\"/>\n" +
                "    </process>\n" +
                "\n" +
                "</process-context>";
        ProcessEngine processEngine = new ProcessEngine(new StringXmlProcessParser(xmlConfig));
        ProcessContext process1 = processEngine.getContext("process-async");
        process1.set("nextId", "node4");
        process1.start();
    }


}
